package com.yanq.liancar.adapter;
/*
 *  @创建者     Administrator
 *  @创建时间   2018/11/27  11:13
 *  @描述       ${TODO}
 */


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yanq.liancar.R;
import com.yanq.liancar.bean.ReplyAllBean;

import java.util.List;

public class ReplyAllAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<ReplyAllBean.DataBean.CommentsRepliesBean> mList;
    private final int EMPTY_VIEW = 1;
    private OnItemClickListener clickListener;
    private final int PROGRESS_VIEW = 2;

    public ReplyAllAdapter(Context context, List<ReplyAllBean.DataBean.CommentsRepliesBean> mList) {
        this.mContext = context;
        this.mList = mList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == EMPTY_VIEW) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_replyall1, parent, false);
            return new MyViewHolder1(view);
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_replyall2, parent, false);
            return new MyViewHolder2(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
        if (mList != null) {
            if (holder instanceof MyViewHolder1) {
                final MyViewHolder1 holder1 = (MyViewHolder1) holder;
                holder1.name.setText(mList.get(position).getRepliername() + ":");
                holder1.content1.setText(mList.get(position).getReplycontent());
                if (clickListener != null) {
                    ((MyViewHolder1) holder).name.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            clickListener.onItemClick(position, holder1.name);
                        }
                    });
                }
            } else if (holder instanceof MyViewHolder2) {
                final MyViewHolder2 holder2 = (MyViewHolder2) holder;
                holder2.name1.setText(mList.get(position).getRepliername());
                holder2.name2.setText(mList.get(position).getObjectname() + ":");
                holder2.content2.setText(mList.get(position).getReplycontent());

                if (clickListener!=null){
                    holder2.name1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            clickListener.onItemClick(position,holder2.name1);
                        }
                    });

                    holder2.name2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            clickListener.onItemClick(position,holder2.name2);
                        }
                    });
                }
            }

        }

    }

    //        @Override
    //        public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
    //            if (mList!=null){
    //                holder.mTextView.setText(mList.get(position).getSmall_classifyname());
    //                holder.itemView.setOnClickListener(new View.OnClickListener() {
    //                    @Override
    //                    public void onClick(View view) {
    //                        Bundle bundle = new Bundle();
    //                        bundle.putString("where", "SmallSelectClassAdapter");
    //                        bundle.putInt("Bid", id);
    //                        bundle.putInt("Sid", mList.get(position).getSmallclassifyid());
    //                        RxActivityTool.skipActivity(mContext, SmallBEJGoodsListActivity.class, bundle);
    //                    }
    //                });
    //            }
    //        }

    @Override
    public int getItemCount() {
        return (mList == null ? 0 : mList.size());
    }

    @Override
    public int getItemViewType(int position) {
        if (mList.get(position).getReplytype() == 1) {
            return EMPTY_VIEW;
        } else if (mList.get(position).getReplytype() == 2) {
            return PROGRESS_VIEW;
        } else {
            return super.getItemViewType(position);
        }
    }

    class MyViewHolder1 extends RecyclerView.ViewHolder {
        TextView name;
        TextView content1;

        public MyViewHolder1(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.tv_name);
            content1 = itemView.findViewById(R.id.tv_content);
        }
    }

    class MyViewHolder2 extends RecyclerView.ViewHolder {
        TextView name1;
        TextView name2;
        TextView content2;

        public MyViewHolder2(View itemView) {
            super(itemView);
            name1 = itemView.findViewById(R.id.tv_name1);
            name2 = itemView.findViewById(R.id.tv_name2);
            content2 = itemView.findViewById(R.id.tv_content);
        }
    }

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, View view);
    }
}
